<?php 
$show_validator = true;
include TMPL_PATH.'public/header.phtml';
?>
<script type="text/javascript" src="/statics/js/selectdate.js"></script>
<div class="subnav">
    <div class="content-menu ib-a blue line-x">
        <a href='<?php echo U('activity/seckill');?>' ><em>秒杀活动列表</em></a><span>|</span>
        <a href='javascript:;' class="on"><em><?php if(isset($seckill)) echo '编辑';else echo'添加';?>秒杀活动</em></a><span>|</span>
		<a href='<?php echo U('Activity/seckill_result')?>'><em>秒杀活动结果</em></a>
    </div>
</div>
<style type='text/css'>
.pic {
    float: left;
    margin-right: 10px;
    text-align: center;
}
.pic img {
    border: 3px solid #EFEFED;
    cursor: pointer;
}
.pic img.current {
    border: 3px solid #FF6600;
}
.input-text { 
    text-align:center;
}
</style>
<form name="myform" id="myform" action="__SELF__" method="post" enctype="multipart/form-data">
<div class="pad-10">
    <div class="col-tab">
        <ul class="tabBut cu-li">
            <li id="tab_setting_1" class="on" onclick="SwapTab('setting','on','',<?php echo $langCount;?>,1);">基本选项</li>
        </ul>
    </div>
<div id="div_setting_1" class="contentList pad-10">
<table width="100%" class="table_form ">
      <tr>
        <th></th>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <th></th>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <th></th>
        <td></td>
        <td></td>
      </tr>      
      <tr>
          <th width="10%">秒杀活动开始时间：</th>
          <td width="30%">
          <input name="start_time" type="text" id="start_time" class="input-text" value="<?php if($seckill['start_time']) echo date('Y-m-d H:i:s', $seckill['start_time']);?>" onclick="SelectDate(this,'yyyy-MM-dd hh:mm:ss',0,-150)" readonly="true" style="width:160px;">
         </td>
         <td id="start_time_str"></td>
      </tr>
      <tr>
        <th>秒杀时间：</th>
        <td>
        <input type="text" name="end_time_i" id="end_time_i" class="input-text" size="5" value="<?php echo $seckill['i'];?>"/>
        分
        <input type="text" name="end_time_s" id="end_time_s" class="input-text" size="5" value="<?php echo $seckill['s'];?>"/>
        秒
        </td>
        <td id="end_time_str"></td>
      </tr>
      <tr>
        <td colspan="3">
             <table width="100%" class="table_form " style="text-align:center;">
                  <tr>
                     <th style="text-align:center; width:10%;">上架产品id</th>
                     <th style="text-align:center; width:10%;">最高秒杀套数</th>
                     <th style="text-align:center; width:10%;">秒杀价格</th>
                     <th style="text-align:center; width:24px;"><span id="tr_num"><?php if($seckill_items){ echo count($seckill_items); }else{ echo 1; } ?></span></th>
                     <th style="text-align:left;"></th>
                  </tr>
                  <?php if(!$seckill_items): ?>
                  <tr class="tr">
                     <td>
                     <input type="text" name="id[]" class="input-text id" size="8" value=""/>
                     </td>
                     <td>
                     <input type="text" name="qty[]"  class="input-text qty" size="8" value=""/>套
                     </td>
                     <td>
                     <input type="text" name="price[]" class="input-text price" size="8" value=""/>$
                     </td>
                     <td onclick="del(this);"><a href="javascript:;">删除</a></td>
                     <td class="chk" style="text-align:left;">
                     <input type="hidden" name="oldid[]" class="input-text id" size="8" value=""/>
                     </td>
                  </tr>
                  <?php endif; ?>
                  <?php foreach($seckill_items as $k=>$v): ?>
                  <tr class="tr">
                     <td>
                     <input type="text" name="id[]" class="input-text id" size="8" value="<?php echo $v['pid'];?>"/>
                     </td>
                     <td>
                     <input type="text" name="qty[]"  class="input-text qty" size="8" value="<?php echo $v['qty'];?>"/>套
                     </td>
                     <td>
                     <input type="text" name="price[]" class="input-text price" size="8" value="<?php echo $v['price'];?>"/>$
                     </td>
                     <td onclick="del(this);"><a href="javascript:;">删除</a>
                     <input type="hidden" name="oldid[]" class="input-text oldid" size="8" value="<?php echo $v['id'];?>"/>
                     </td>
                     <td class="chk" style="text-align:left;">
                     </td>
                  </tr>
                  <?php endforeach;?>
                  <tr id="q">
                     <td colspan="2">
                     </td>
                     <td style="text-align:left;">
                        <a href="javascript:more(1);">+追加新的产品</a>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="javascript:more(5);">+再来5个</a>
                     </td>
                     <td>
                     </td>
                     <td>
                     </td>
                  </tr>            
             </table>
        </td>
      </tr>
	</table>
 </div>
    <div class="bk15" style="padding-left:300px;">
        <input name="fid" type="hidden" value="<?php echo $seckill['id'];?>" id="fid" />
        <input name="oldiddel" type="hidden" value="" id="oldiddel" />
        <input name="dosubmit" type="submit" value="提交" class="button" id="button" />
    </div>
</div> 
</form>
<input type="hidden" value="" id="timeverify" />
</body>
</html>
<script type="text/javascript" >
    
	var num_reg=/^\d+$/;
	var price_reg=/^\d+(\.\d{1,2})?$/;
	
	$('#button').click(function(){
		
	  var yesorno=true;
	  
	  if($('#start_time').val()==''){
	      yesorno=false;
		  $('#start_time_str').html('').html("<font color='red'>请选择秒杀活动开始时间</font>"); 
	  }else{
		  $('#start_time_str').html('');  
	  }
	  
	  var i = $('#end_time_i').val();
	  var s = $('#end_time_s').val();
	  if(i == '' || s == ''){
	      yesorno=false;
		  $('#end_time_str').html('').html("<font color='red'>请输入秒杀时间</font>"); 
	  }else if( !num_reg.test(i) || !num_reg.test(s) || s>59 ){
	      yesorno=false;
		  $('#end_time_str').html('').html("<font color='red'>请输入正确秒杀时间</font>"); 
	  }else{
		  $('#end_time_str').html('');  
	  }	  	  
	  
	  var len=$('.tr').length;
	  for(var i=0;i<len;i++){
		  
	      var id=$('.id').eq(i).val();
		  var qty=$('.qty').eq(i).val();
		  var price=$('.price').eq(i).val();
 		  //alert(id);alert(qty);alert(qty);return false;
		  var errstr="";
		  if(id==''){
		      yesorno=false;
		      errstr+='请输入产品id!　';
		  }else if(!num_reg.test(id)){
		      yesorno=false;
			  errstr+='产品id输入错误!　'; 
		  }else{
		      var isbeing=true;
		      for(var k=0;k<i;k++){
				  if(id == $('.id').eq(k).val()){
						isbeing=false;yesorno=false; 
						errstr+='页面上已有相同的产品ID!　'; 
						break;
				  }
			  }
		  }

		  if(qty==''){
		      yesorno=false;
		      errstr+='请输入秒杀套数!　';
		  }else if(!num_reg.test(qty) || qty < 1){
		      yesorno=false;
			  errstr+='秒杀套数输入错误!　'; 
		  }
		  
		  if(price==''){
		      yesorno=false;
		      errstr+='请输入秒杀价格!　';
		  }else if(!price_reg.test(price) || price < 0){
		      yesorno=false;
			  errstr+='秒杀价格输入错误!　'; 
		  }
		  
		  $('.chk').eq(i).html('').html("<font color='red'>"+errstr+"</font>"); 
	  }
	  
	  if(yesorno){
		  timeverify();
		  if($('#timeverify').val() != '1'){ alert($('#timeverify').val());return false; }
	  }
      return yesorno;
	  
	})
	function timeverify(){
		
	  var start_time = $('#start_time').val();
	  var          i = $('#end_time_i').val();
	  var          s = $('#end_time_s').val();
	  var          fid = $('#fid').val();	
		
	  	var successProcess=function(data){
            $('#timeverify').val(data);	
		}
		$.ajax({
			type: 'POST',
			url: "<?php echo U('activity/seckill_pro');?>",
			data: {'timeverify':1,'start_time':start_time,'i':i,'s':s,'fid':fid},
			cache:false,
			async:false,
			success: successProcess
		})	    	
	}
	 
	function more(num){
		
		for(var i=0; i<num ;i++){
			var tr_num = $('.tr').length;
			//if( tr_num >= 50 ){ alert('每次最多添加50个商品!');return; };		
			
			$('#tr_num').text(tr_num+1);
			var tr  = '<tr class="tr">';
				tr += '<td><input type="text" name="id[]" class="input-text id" size="8" value=""/></td>';
				tr += '<td><input type="text" name="qty[]"  class="input-text qty" size="8" value=""/>套</td>';
				tr += '<td><input type="text" name="price[]" class="input-text price" size="8" value=""/>$</td>';
				tr += '<td onclick="del(this);"><a href="javascript:;">删除</a><input type="hidden" name="oldid[]" class="input-text oldid" size="8" value=""/></td>';
				tr += '<td class="chk" style="text-align:left;"></td></tr>';
			
			var $tr=$(tr); 
			$tr.insertBefore('#q');
		}   
	}
	function del(obj){ 
	   var oldid = $('.oldid').eq($('.tr').index($(obj).parent())).val();
	   if(oldid!=""){ $('#oldiddel').val($('#oldiddel').val()+','+oldid); }
	   //return;
	   $(obj).parent().remove();$('#tr_num').text($('.tr').length);
	}

</script>